<template>
  <div>
    <!-- 按钮 -->
    <el-button type="primary" style="margin: 10px 0px;" icon="el-icon-plus" @click="showDialog">添加</el-button>
    <el-table style="width: 100%;" border :data="list">
      <el-table-column type="index" label="序号" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称" width="width">
      </el-table-column>
      <el-table-column prop="prop" label="品牌Logo" width="width">
        <template slot-scope="{row,$index}">
          <img :src="row.logoUrl" alt="品牌logo" style="width: 70px;height: 45px;">
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{row,$index}">
          <el-button type="warning" size="mini" @click="updateTradeMark(row)" icon="el-icon-edit">修改</el-button>
          <el-button type="danger" size="mini" icon="el-icon-detele" @click="deleteTradeMark(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- @size-change="sizeChange"
      @current-change="currentChange" -->
    <!-- 当前页数current-page
        总页数total
        每页条数page-size,page-sizes可以设置每页展示多少条数据
        连续页码pager-count  如果是9 则连续页码为7   因为最前和最后也算
        展示顺序layout    ->可以让到最右边进行展示
       -->
    <!-- 分页列表 -->
    <el-pagination style="margin-top: 20px;text-align: center;" :current-page="page" :total="total" :page-size="limit"
      :page-sizes="[3, 5, 8, 10]" :pager-count="5" @size-change="sizeChange" @current-change="getPageList"
      layout=" prev, pager, next, jumper,->,sizes,total">
    </el-pagination>
    <!-- 对话框 -->
    <el-dialog :title="tmForm.id ? '修改品牌' : '添加品牌'" :visible.sync="dialogFormVisible">
      <!-- form表单 :model属性,这个属性可以将表单数据收集到那个对象身上,用来做表单验证 -->
      <el-form style="width: 80%;" :model="tmForm" :rules="rules" ref="form">
        <el-form-item label="品牌名称" :label-width="'100px'" prop="tmName">
          <el-input autocomplete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" :label-width="'100px'" prop="logoUrl">
          <!--  -->
          <!-- 这里收集数据不能使用vmodel, 因为不是表单元素
              action:这里设置图片上传的地址
            on-success:检测图片上传成功,当图片上传成功会执行一次
            before-upload:可以在图片上传之前执行一次
          -->
          <el-upload class="avatar-uploader" action="/dev-api/admin/product/fileUpload" :show-file-list="false"
            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">只能上传jpg文件，且不超过1M</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTradeMark">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
export default {
  name: 'tradeMark',
  data() {
    return {
      //代表分页器第几页
      page: 1,
      //当前页数展示条数
      limit: 5,
      //总页数
      total: 0,
      //品牌数组
      list: [],
      //控制对话框显示隐藏
      dialogFormVisible: false,
      //收集品牌信息,对象身上的属性,不能瞎写
      tmForm: {
        tmName: '',
        logoUrl: ''
      },
      //表单验证规则
      rules: {
        //品牌名称
        tmName: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'change' }
        ],
        //品牌logo
        logoUrl: [
          { required: true, message: '请选择品牌的图片' }
        ],
      }
    }
  },
  mounted() {
    this.getPageList()
  },
  methods: {
    async getPageList(pager = 1) {
      this.page = pager
      const { page, limit } = this//解构出参数
      let result = await this.$API.trademark.reqTradeMarkList(page, limit)//代表给服务器传递参数
      if (result.code == 200) {
        //分别是展示列表的总条数以及列表中展示的数据
        this.total = result.data.total
        this.list = result.data.records
      }
    },
    sizeChange(limit) {
      this.limit = limit
      this.getPageList(this.page)
    },
    //展示对话框
    showDialog() {
      this.dialogFormVisible = true
      this.tmForm = { tmName: '', logoUrl: '' }
    },
    //修改某一个品牌
    updateTradeMark(row) {
      //row当前用户选中那一行
      this.dialogFormVisible = true
      this.tmForm = { ...row }
    },
    //上传图片的回调
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || 'image/gif';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    addOrUpdateTradeMark() {
      //当全部字段通过后,再去书写业务逻辑
      this.$refs.form.validate(async (success) => {
        //如果全部字段符合条件
        if (success) {
          this.dialogFormVisible = false
          //发请求(添加品牌||修改品牌)
          let result = await this.$API.trademark.reqAddOrUpdateTradeMark(this.tmForm);
          if (result.code == 200) {
            //弹出信息说明成功
            this.$message({ type: "success", message: this.tmForm.id ? '修改品牌成功' : '添加品牌成功' })
            this.getPageList(this.tmForm.id ? this.page : 1)
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      })
    },
    deleteTradeMark(row) {
      this.$confirm(`你是否确定删除${row.tmName}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        //当用户点击确定按钮的是触发
        let result = await this.$API.trademark.reqDeteleTradeMark(row.id)
        if (result.code == 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getPageList(this.list.length>1?this.page:this.page-1)
        }
      }).catch(() => {
        //当用户点击取消的时候会触发
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }

}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
